﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Data;
//using System.Windows.Documents;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Imaging;
//using System.Windows.Navigation;
//using System.Windows.Shapes;
//using System.Reflection;
//using System.ComponentModel;
//using KHIS.PublicTypes;
//using KHIS.PublicTypes.Tracker;
//using KHIS.ClientApp.Common;

namespace KHIS.ClientApp
{
    /*
    /// <summary>
    /// Interaction logic for ObjectViewer.xaml
    /// </summary>
    public partial class ObjectViewer : UserControl
    {
        /// <summary>
        /// Retrieve resource list delegate.
        /// </summary>
        public OnNeedResourceList NeedResourceList
        {
            get;
            set;
        }

        /// <summary>
        /// Save special modification.
        /// </summary>
        public SaveModifyList SaveModification
        {
            get;
            set;
        }

        public Window Me { get; set; }

        public ObjectViewer()
        {
            InitializeComponent();
            DataContextChanged += new DependencyPropertyChangedEventHandler(ObjectViewer_DataContextChanged);
        }

        private void SaveChangesAction(ActionListObject actions)
        {
            if (SaveModification != null)
            {
                SaveModification(actions, this.DataContext);
            }
        }

        public List<object> NeedList(Type type, object source, object param)
        {
            if (NeedResourceList != null)
            {
                if (source == null)
                {
                    source = DataContext;
                }
                return NeedResourceList(type, source, param);
            }
            return null;
        }

        void ObjectViewer_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                List<MemberProperty> props = null;
                Type type = e.NewValue.GetType();
                ObjectToPanelModels.AllMemberPropertyConfig.TryGetValue(type, out props);
                UIElement element = null;
                if (props != null)
                {
                    element = Convert(type, props);
                }
                Content = element;

                if (IsReadOnly)
                {
                    SetReadOnly(element, false);
                }
            }
            else
            {
                Content = null;
            }
        }

        private void SetReadOnly(UIElement ui, bool enabled)
        {
            if (ui is Decorator)
            {
                SetReadOnly((ui as Decorator).Child, enabled);
            }
            else if (ui is Panel)
            {
                foreach (UIElement item in (ui as Panel).Children)
                {
                    SetReadOnly(item, enabled);
                }
            }
            else if (ui is ContentControl && (ui as ContentControl).Content is UIElement)
            {
                SetReadOnly(((ui as ContentControl).Content as UIElement), enabled);
            }
            else if (ui is TextBox)
            {
                (ui as TextBox).IsReadOnly = !enabled;
            }
            else if (ui is Button)
            {
                (ui as Button).IsEnabled = enabled;
            }
        }


        private bool _readOnly = false;
        public bool IsReadOnly
        {
            set
            {
                _readOnly = value;
            }
            get { return _readOnly; }
        }

        #region UI Generator
        private Type parentType = null;

        public UIElement Convert<T>(IList<MemberProperty> priority)
        {
            return Convert(typeof(T), priority);
        }

        public UIElement Convert(Type objType, IList<MemberProperty> priority)
        {
            parentType = objType;

            Grid grid = new Grid();

            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Auto) });
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            foreach (MemberProperty memProp in priority)
            {
                PropertyInfo info = objType.GetProperty(memProp.Name);
                if (info == null)
                {
                    continue;
                }

                RowDefinition row = new RowDefinition();
                if (!memProp.IsFixVertical && !info.PropertyType.IsGenericType)
                {
                    row.Height = new GridLength(30);
                }
                grid.RowDefinitions.Add(row);

                Label label = new Label();
                label.Content = memProp.Name + ":";
                label.HorizontalContentAlignment = HorizontalAlignment.Right;

                grid.Children.Add(label);
                Grid.SetRow(label, grid.RowDefinitions.Count - 1);
                Grid.SetColumn(label, 0);

                UIElement subPanel = GetCustomedPanel(info.PropertyType, memProp);
                if (!info.CanWrite)
                {
                    subPanel.IsEnabled = false;
                }
                grid.Children.Add(subPanel);
                Grid.SetRow(subPanel, grid.RowDefinitions.Count - 1);
                Grid.SetColumn(subPanel, 1);
            }

            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(30) });
            Button saveButton = new Button();
            SaveButton = saveButton;
            saveButton.Content = "Save";
            saveButton.Width = 100;
            saveButton.Visibility = Visibility.Hidden;
            saveButton.Margin = new Thickness(3);
            saveButton.Click += new RoutedEventHandler(saveButton_Click);
            saveButton.HorizontalAlignment = HorizontalAlignment.Right;
            grid.Children.Add(saveButton);
            Grid.SetRow(saveButton, grid.RowDefinitions.Count - 1);
            Grid.SetColumn(saveButton, 1);

            ScrollViewer viewer = new ScrollViewer();
            viewer.Content = grid;
            viewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            return viewer;
        }

        private Button SaveButton;

        struct SourceAndTarget
        {
            public MemberProperty Property;
            public object Source;
            public Type ObjectType;
            public object Target;
        }

        private ActionListObject ListObject = new ActionListObject()
        {
            { ModifyAction.Add, new List<Object>()},
            {ModifyAction.Remove, new List<Object>()},
            {ModifyAction.Modify , new List<Object>()}
        };

        private UIElement GetCustomedPanel(Type type, MemberProperty property)
        {
            UIElement antherControl;



            if (type == typeof(bool))
            {
                //ComboBox comboBox = new ComboBox();
                //comboBox.Items.Add(true);
                //comboBox.Items.Add(false);

                //comboBox.SetBinding(ComboBox.SelectedItemProperty, property.Name);
                //comboBox.Name = property.Name;
                //comboBox.Margin = new Thickness(3);

                CheckBox checkBox = new CheckBox();
                checkBox.SetBinding(CheckBox.IsCheckedProperty, property.Name);
                checkBox.Name = property.Name;
                checkBox.Margin = new Thickness(3);
                checkBox.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                checkBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

                antherControl = checkBox;
            }
            else if (type.Name.StartsWith("List`1"))
            {
                Grid grid = new Grid();

                DockPanel dockPanel = new DockPanel();
                dockPanel.LastChildFill = true;

                ListBox listbox = new ListBox();
                listbox.Name = property.Name;
                listbox.SetBinding(ListBox.ItemsSourceProperty, property.Name);
                listbox.KeyDown += new System.Windows.Input.KeyEventHandler(listbox_KeyDown);
                listbox.MouseDoubleClick += new MouseButtonEventHandler(listbox_MouseDoubleClick);
                listbox.Margin = new Thickness(3);
                listbox.VerticalAlignment = VerticalAlignment.Stretch;
                listbox.MinHeight = 50;
                SourceAndTarget sat = new SourceAndTarget();
                sat.Property = property;
                listbox.Tag = sat;

                DockPanel comboBoxDockPanel = new DockPanel();
                comboBoxDockPanel.LastChildFill = true;

                ComboBox box = new ComboBox();
                box.Tag = type;
                box.Margin = new Thickness(3);

                if (type.IsGenericType)
                {
                    Type[] param = type.GetGenericArguments();
                    Type first = param[0];
                    if (first.IsPrimitive || first == typeof(string))
                    {
                        box.IsEditable = true;
                        box.KeyDown += new System.Windows.Input.KeyEventHandler(box_KeyDown);
                    }
                    else
                    {

                        box.DropDownOpened += new EventHandler(box_DropDownOpened);

                        SourceAndTarget satlistBox = new SourceAndTarget();
                        satlistBox.Source = box;
                        satlistBox.Target = listbox;
                        satlistBox.ObjectType = first;
                        satlistBox.Property = property;

                        box.Tag = satlistBox;

                        Button addButton = new Button();
                        addButton.Content = "+";
                        addButton.Tag = satlistBox;
                        addButton.Width = 30;
                        addButton.Margin = new Thickness(3);
                        addButton.Click += new RoutedEventHandler(addButton_Click);

                        comboBoxDockPanel.Children.Add(addButton);
                        DockPanel.SetDock(addButton, Dock.Right);
                    }
                }
                comboBoxDockPanel.Children.Add(box);

                dockPanel.Children.Add(comboBoxDockPanel);
                dockPanel.Children.Add(listbox);

                DockPanel.SetDock(comboBoxDockPanel, Dock.Top);
                antherControl = dockPanel;
            }
            else if (type.IsPrimitive || type == typeof(string))
            {
                TextBox textBox = new TextBox();
                textBox.SetBinding(TextBox.TextProperty, property.Name);
                textBox.Name = property.Name;
                textBox.Margin = new Thickness(3);
                textBox.IsReadOnly = !property.IsEditable;
                if (property.IsFixVertical)
                {
                    textBox.TextWrapping = TextWrapping.Wrap;
                    textBox.MinHeight = 80;
                    textBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                }
                antherControl = textBox;
            }
            else
            {
                Label label = new Label();
                label.SetBinding(Label.ContentProperty, property.Name);
                label.Margin = new Thickness(3);
                label.BorderBrush = Brushes.LightBlue;
                label.BorderThickness = new Thickness(1);
                label.Name = property.Name;
                label.Foreground = Brushes.Blue;
                label.VerticalContentAlignment = VerticalAlignment.Center;
                label.Padding = new Thickness(0);
                label.Cursor = System.Windows.Input.Cursors.Hand;
                label.MouseLeftButtonDown += new MouseButtonEventHandler(label_MouseDown);

                SourceAndTarget sat = new SourceAndTarget();
                sat.ObjectType = type;
                sat.Property = property;

                label.Tag = sat;

                antherControl = label;
            }
            if (property.IsNeeded && antherControl is Control)
            {
                (antherControl as Control).BorderBrush = Brushes.Orange;
            }
            return antherControl;
        }

        void listbox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            Object item = listBox.SelectedItem;
            SourceAndTarget sat = (SourceAndTarget)listBox.Tag;
            if (item != null)
            {
                ViewObjectWindow viewWindow = new ViewObjectWindow(NeedResourceList, item.GetType(), parentType, null, item);
                viewWindow.Owner = Me;
                viewWindow.IsEnabled = sat.Property.IsEditable;
                viewWindow.SetModify();
                if (viewWindow.ShowDialog() == true && viewWindow.SelectedObject != null)
                {
                    item = viewWindow.SelectedObject;
                    BindingExpression expression = listBox.GetBindingExpression(ListBox.ItemsSourceProperty);
                    expression.UpdateTarget();
                    listBox.Items.Refresh();

                    KeyValuePair<object, object> pair = new KeyValuePair<object, object>(null, item);

                    DisdinctValue(ModifyAction.Modify, pair);
                }
            }
        }

        void saveButton_Click(object sender, RoutedEventArgs e)
        {
            SaveChangesAction(ListObject);

            ListObject[ModifyAction.Add].Clear();
            ListObject[ModifyAction.Remove].Clear();
            ListObject[ModifyAction.Modify].Clear();
            SaveButton.IsEnabled = false;
        }

        void box_DropDownOpened(object sender, EventArgs e)
        {
            ComboBox box = (ComboBox)sender;
            box.Items.Clear();
            SourceAndTarget sat = (SourceAndTarget)box.Tag;
            Type type = sat.ObjectType;
            List<Object> objList = NeedResourceList(type, parentType, sat.Property);
            foreach (var item in objList)
            {
                box.Items.Add(item);
            }
        }


        void addButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            SourceAndTarget sat = (SourceAndTarget)button.Tag;
            ComboBox comboBox = sat.Source as ComboBox;
            ListBox listBox = sat.Target as ListBox;
            Object selectedObj = null;
            if (comboBox.SelectedItem != null)
            {
                selectedObj = comboBox.SelectedItem;

            }
            else
            {
                ViewObjectWindow window = new ViewObjectWindow(NeedResourceList, sat.ObjectType, parentType, sat.Property, null);
                window.Owner = Me;
                if (window.ShowDialog() == true)
                {
                    selectedObj = window.SelectedObject;
                }
            }
            if (selectedObj == null)
            {
                return;
            }
            BindingExpression expression = listBox.GetBindingExpression(ListBox.ItemsSourceProperty);
            string property = expression.ParentBinding.Path.Path;
            PropertyInfo listProperty = listBox.DataContext.GetType().GetProperty(property);
            Object listObj = listProperty.GetValue(listBox.DataContext, null);
            if (listObj == null)
            {
                listObj = listProperty.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });
            }
            listProperty.PropertyType.GetMethod("Add").Invoke(listObj, new object[] { selectedObj });
            expression.UpdateTarget();
            listBox.Items.Refresh();

            if (!sat.Property.IsSpecialProperty)
            {
                DisdinctValue(ModifyAction.Add, selectedObj);
            }
        }

        void label_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            Object originObj = label.Content;
            SourceAndTarget sat = (SourceAndTarget)label.Tag;
            Type type = sat.ObjectType;
            MemberProperty memProp = sat.Property;
            ViewObjectWindow window = new ViewObjectWindow(NeedResourceList, type, parentType, memProp, label.Content);
            window.Owner = Me;
            if (window.ShowDialog() == true)
            {
                Object obj = window.SelectedObject;
                BindingExpression expression = label.GetBindingExpression(Label.ContentProperty);
                string property = expression.ParentBinding.Path.Path;
                label.DataContext.GetType().GetProperty(property).SetValue(label.DataContext, obj, null);
                expression.UpdateTarget();

                if (memProp.IsSpecialProperty)
                {
                    KeyValuePair<object, object> pair = new KeyValuePair<object, object>(originObj, obj);
                    DisdinctValue(ModifyAction.Modify, pair);
                }
            }
        }

        void box_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        void listbox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            object item = listBox.SelectedItem;
            if (e.Key == Key.Delete && item != null)
            {
                BindingExpression expression = listBox.GetBindingExpression(ListBox.ItemsSourceProperty);
                string property = expression.ParentBinding.Path.Path;
                PropertyInfo listProperty = listBox.DataContext.GetType().GetProperty(property);
                Object listObj = listProperty.GetValue(listBox.DataContext, null);
                listProperty.PropertyType.GetMethod("Remove").Invoke(listObj, new object[] { item });
                expression.UpdateTarget();
                listBox.Items.Refresh();
            }
            DisdinctValue(ModifyAction.Remove, item);
        }

        void DisdinctValue(ModifyAction action, object item)
        {
            bool has = false;
            switch (action)
            {
                case ModifyAction.Add:
                    if (ListObject[ModifyAction.Remove].Contains(item))
                    {
                        ListObject[ModifyAction.Remove].Remove(item);
                        has = true;
                    }
                    break;
                case ModifyAction.Remove:
                    if (ListObject[ModifyAction.Add].Contains(item))
                    {
                        ListObject[ModifyAction.Add].Remove(item);
                        has = true;
                    }
                    break;
                case ModifyAction.Modify:
                    has = ListObject[ModifyAction.Add].Contains(item) || ListObject[ModifyAction.Remove].Contains(item);
                    break;
                default:
                    break;
            }

            if (!has)
            {
                ListObject[action].Add(item);
            }

            SaveButton.Visibility = !(ListObject[ModifyAction.Add].Count == 0 &&
                ListObject[ModifyAction.Modify].Count == 0 &&
                ListObject[ModifyAction.Remove].Count == 0) ? Visibility.Visible : Visibility.Hidden;

            SetSaveBottonTooltip();

        }

        private void SetSaveBottonTooltip()
        {
            string tooltip = "";
            string buttonName = "";
            foreach (var item in ListObject)
            {
                if (item.Value.Count == 0)
                {
                    continue;
                }
                buttonName += item.Key + "[" + item.Value.Count + "]";
                tooltip += item.Key.ToString() + ": ";
                foreach (var action in item.Value)
                {
                    if (action is KeyValuePair<object, object>)
                    {
                        tooltip += ((KeyValuePair<object, object>)action).Value.ToString() + ",";
                    }
                    else
                    {
                        tooltip += action.ToString() + ",";
                    }
                }
                tooltip += Environment.NewLine;
            }
            SaveButton.Content = buttonName;
            SaveButton.ToolTip = tooltip;
        }

        #endregion

    }*/
}

































//<Window x:Class="KHIS.ClientApp.ViewObjectWindow"
//        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
//        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
//        xmlns:local="clr-namespace:KHIS.ClientApp"
//        Title="ViewObjectWindow" SizeToContent="WidthAndHeight" MinWidth="600"  ShowInTaskbar="False">
//    <Grid>
//        <Grid.RowDefinitions>
//            <RowDefinition Height="Auto" />
//            <RowDefinition Height="348*" />
//            <RowDefinition Height="36" />
//        </Grid.RowDefinitions>
//        <ComboBox HorizontalAlignment="Left" Margin="4" Name="comboBoxTypes" Width="106" ItemsSource="{Binding ItemTypes}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedItemType}"/>
//        <ComboBox Margin="115,4,4,4" Name="ObjectList"  ItemsSource="{Binding ItemsList}" SelectedItem="{Binding SelectedObject}" TextBoxBase.TextChanged="ObjectList_TextChanged"/>
//        <local:ObjectViewer Grid.Row="1" Margin="3" DataContext="{Binding SelectedObject}" x:Name="objectView"/>
//        <Button Margin="8,5,0,8" HorizontalAlignment="Left" Content="Null" Width="50" Foreground="Red" Grid.Row="2" Name="NullButton" Click="NullButton_Click"/>
//        <Button Margin="70,5,0,8" HorizontalAlignment="Left" Content="New" Width="50" Foreground="Blue" Grid.Row="2" Name="NewButton" Click="NewButton_Click"/>
//        <Button Margin="0,5,38,8" HorizontalAlignment="Right" Content="Cancel" Grid.Row="2" Width="50" Click="Cancel_Click"/>
//        <Button Margin="0,5,120,8" HorizontalAlignment="Right" Content="OK" Grid.Row="2" Width="50" Click="OK_Click" />
//    </Grid>
//</Window>











































//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Data;
//using System.Windows.Documents;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Imaging;
//using System.Windows.Shapes;
//using System.Xml.Serialization;
//using KHIS.ClientApp.Common;

//namespace KHIS.ClientApp
//{
//    /// <summary>
//    /// Interaction logic for ViewObjectWindow.xaml
//    /// </summary>
//    public partial class ViewObjectWindow : Window
//    {
//        public ViewObjectWindow(OnNeedResourceList needList, Type objectType, Type parentType , MemberProperty memberProperty , object defaultValue)
//        {
//            InitializeComponent();

//            if (needList == null)
//            {
//                ObjectList.Visibility = System.Windows.Visibility.Collapsed;
//                comboBoxTypes.Visibility = System.Windows.Visibility.Collapsed;
//            }

//            objectView.NeedResourceList = needList;
//            objectView.Me = this;
//            this._parentType = parentType;
//            this._memberProperty = memberProperty;

//            if (memberProperty != null)
//            {
//                Title = memberProperty.Name; 
//            }

//            ItemTypes = new List<Type>();
//            if (!objectType.IsAbstract)
//            {
//                ItemTypes.Add(objectType);
//            }
//            else
//            {
//                object[] attrs = objectType.GetCustomAttributes(false);
//                foreach (var item in attrs)
//                {
//                    if (item is XmlIncludeAttribute)
//                    {
//                        ItemTypes.Add((item as XmlIncludeAttribute).Type);
//                    }
//                }
               
//            }
//            if (ItemTypes.Count != 0)
//            {
//                comboBoxTypes.GetBindingExpression(ComboBox.ItemsSourceProperty).UpdateTarget();
//                SelectedItemType = ItemTypes[0];
//            }
//            else
//            {
//                SelectedItemType = objectType;
//            }

//            if (defaultValue != null)
//            {
//                SelectedObject = defaultValue;
//            }

//            DataContext = this;
//        }

//        public string GetChineseSpell(string strText) 
//        { 
//            int len = strText.Length; 
//            string myStr = ""; 
//            for (int i = 0; i < len; i++) 
//            { 
//                myStr += getSpell(strText.Substring(i, 1)); 
//            } 
//            return myStr; 
//        } 
 
//        public string getSpell(string cnChar) 
//        { 
//            byte[] arrCN = Encoding.Default.GetBytes(cnChar); 
//            if (arrCN.Length > 1) 
//            { 
//                int area = (short)arrCN[0]; 
//                int pos = (short)arrCN[1]; 
//                int code = (area << 8) + pos; 
//                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906,   51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 }; 
//                for (int i = 0; i < 26; i++) 
//                { 
//                    int max = 55290; 
//                    if (i != 25) max = areacode[i + 1]; 
//                    if (areacode[i] <= code && code < max) 
//                    { 
//                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) }); 
//                    } 
//                } 
//                return "*"; 
//            } 
//            else 
//                return cnChar; 
//        } 

//        private Type _parentType = null;
//        private MemberProperty _memberProperty;

//        private object _selectedObject = null;
//        public object SelectedObject 
//        { 
//            get
//            { 
//                return _selectedObject; 
//            }
//            set
//            {
//                _selectedObject = value;
//                ObjectList.GetBindingExpression(ComboBox.SelectedItemProperty).UpdateTarget();
//                objectView.GetBindingExpression(ObjectViewer.DataContextProperty).UpdateTarget(); 
//            }
//        }

//        public List<Object> ItemsList
//        { 
//            get; 
//            set; 
//        }

//        private Type _selectedItemType = null;
//        public Type SelectedItemType
//        {
//            get
//            {
//                return _selectedItemType;
//            }
//            set
//            {
//                _selectedItemType = value;
//                SelectedObject = SelectedItemType.GetConstructor(new Type[] { }).Invoke(new object[] { });
//                comboBoxTypes.GetBindingExpression(ComboBox.SelectedItemProperty).UpdateTarget();
//                List<Object> objs = objectView.NeedList(value, _parentType, _memberProperty);
//                ItemsList = objs;
//                ObjectList.IsEditable = _selectedItemType == typeof(KHIS.PublicTypes.Code);
//                if (ObjectList.IsEditable)
//                {
//                    ObjectList.GotFocus += new RoutedEventHandler(ObjectList_GotFocus);
//                    ObjectList.LostFocus += new RoutedEventHandler(ObjectList_LostFocus);
//                }
//                ObjectList.GetBindingExpression(ComboBox.ItemsSourceProperty).UpdateTarget();
//            }
//        }


//        public List<Type> ItemTypes
//        {
//            get;
//            set;
//        }

//        private void OK_Click(object sender, RoutedEventArgs e)
//        {
//            DialogResult = true;
//            this.Close();
//        }

//        private void Cancel_Click(object sender, RoutedEventArgs e)
//        {
//            DialogResult = false;
//            this.Close();
//        }

//        public bool IsJustView
//        {
//            set
//            {
//                SetReadOnly(this, !value);
//            }
//        }

//        public void SetModify()
//        {
//            comboBoxTypes.IsEnabled = false;
//            ObjectList.IsEnabled = false;
//            NullButton.IsEnabled = false;
//            NewButton.IsEnabled = false;
//        }

//        private void SetReadOnly(UIElement ui, bool enabled)
//        {
//            //if (ui is Decorator)
//            //{
//            //    SetReadOnly((ui as Decorator).Child, enabled);
//            //}
//            //else if (ui is Panel)
//            //{
//            //    foreach (UIElement item in (ui as Panel).Children)
//            //    {
//            //        SetReadOnly(item, enabled);
//            //    }
//            //}
//            //else if (ui is ContentControl && (ui as ContentControl).Content is UIElement)
//            //{
//            //    SetReadOnly(((ui as ContentControl).Content as UIElement), enabled);
//            //}
//            //else if (ui is ObjectViewer )
//            //{
//            //    (ui as ObjectViewer).IsReadOnly = !enabled;
//            //}
//            //else if (ui is TextBox)
//            //{
//            //    (ui as TextBox).IsReadOnly = !enabled;
//            //}
//            //else if (ui is Button)
//            //{
//            //    (ui as Button).IsEnabled = enabled;
//            //}
//        }

//        private void NullButton_Click(object sender, RoutedEventArgs e)
//        {
//            SelectedObject = null;
//        }

//        private void NewButton_Click(object sender, RoutedEventArgs e)
//        {
//            SelectedObject = SelectedItemType.GetConstructor(new Type[] { }).Invoke(new object[] { });
//        }

//        private void ObjectList_TextChanged(object sender, TextChangedEventArgs e)
//        {
//            ObjectList.Items.Filter = new Predicate<object>(obj =>
//            {
//                if (obj.GetType() == typeof(KHIS.PublicTypes.Code))
//                {
//                    if (obj.ToString().Contains(ObjectList.Text))
//                    {
//                        return true;
//                    }
//                    return GetChineseSpell(obj.ToString()).Contains(ObjectList.Text.ToUpper());
//                }
//                return true;
//            });
//        }

//        private void ObjectList_GotFocus(object sender, RoutedEventArgs e)
//        {
//            ObjectList.IsDropDownOpen = true;
//        }

//        void ObjectList_LostFocus(object sender, RoutedEventArgs e)
//        {
//            ObjectList.IsDropDownOpen = false;
//        }
//    }
//}
